function checkAlphaNumeric(txt){
	  return (txt.match( /[A-Za-z][A-Za-z0-9]*/ ) == txt);
}

function checkNumeric(txt){
	  return (txt.match( /[0-9]+/ ) == txt);
}

function checkAlphabetic(txt){
	  return (txt.match( /[A-Za-z]+/ ) == txt);
}

//Register click handler  
$(document).ready(function(){
  $("#registerBtn").click(function(){
    		//the user name doesn't exist
        	var can_register = true;
    		$("#usernameErr").html("Available!");
    		$("#usernameErr").css("color","#00AA00");
    		
    		//checking username
    		if (!checkAlphaNumeric(document.regForm.username.value)){
    			$("#usernameErr").html("Must be alpha-numeric!");
    			$("#usernameErr").css("color","#FF0000");
    			can_register = false;
    		}
    		
    		//checking sur name
    		if (!checkAlphabetic(document.regForm.surName.value)){
    			$("#surNameErr").html("Must be alphabetic!");
    			can_register = false;
    		}else{
    			$("#surNameErr").html("");
    		}
    		
    		//checking given name
    		if (!checkAlphabetic(document.regForm.givenName.value)){
    			$("#givenNameErr").html("Must be alphabetic!");
    			can_register = false;
    		}else{
    			$("#givenNameErr").html("");
    		}
    		
    		//checking phone number
    		if (!checkNumeric(document.regForm.phoneNumber.value)){
    			$("#phoneNumberErr").html("Must be numeric!");
    			can_register = false;
    		}else{
    			if((document.regForm.phoneNumber.value.length < 9) || (document.regForm.phoneNumber.value.length > 10)){
    				$("#phoneNumberErr").html("Must be 9 or 10 characters");
        			can_register = false;    				
    			}else{
    				$("#phoneNumberErr").html("");
    			}
    		}
    		
    		//checking password
    		if (!checkAlphaNumeric(document.regForm.password1.value)){
    			$("#password1Err").html("Must be alpha-numeric!");
    			can_register = false;
    		}else{
    			if((document.regForm.password1.value.length < 5) || (document.regForm.password1.value.length > 25)){
    				$("#password1Err").html("Must be between 5 or 25 characters");
        			can_register = false;    				
    			}else{
    				$("#password1Err").html("");
    			}
    		}
    		
    		if (document.regForm.password1.value != document.regForm.password2.value){
    			$("#password2Err").html("Passwords don't match");
    			can_register = false;
    		}else{
    			$("#password2Err").html("");
    		}
    		
    		if (can_register){
        		$.post("register", $("#regForm").serialize(),function(data){
        			//making it work with FF......
        			var serializer = new XMLSerializer();
        			var xml = serializer.serializeToString(data);
        			data = xml.replace("<firefoxsucks>","").replace("</firefoxsucks>","");
        			//real begining fo function
        			if (data == "registered"){
        				//alter top menu
        				$("#unloggedMenu").css("display","none");
        				$("#logout").css("display","inline");
        				//reload menu
        				$("#linksmenu").load('contents/side_menu.jsp');
        				//do the same as when 'home' is clicked
        				$("#home").click();
        			}else{
        	    		$("#usernameErr").html("Such username already exists");
        	    		$("#usernameErr").css("color","#FF0000");
        			}
        		});
    		}
  });
  
  $("#checkAvailabilityBtn").click(function(){
	    $.post("checkUserExistance", $("#regForm").serialize(),function(data){
			//making it work with FF......
			data = data.replace("<firefoxsucks>","").replace("</firefoxsucks>","");
			//real begining fo function
	    	if(data == "notExist"){
	    		$("#usernameErr").html("Available!");
	    		$("#usernameErr").css("color","#00AA00");
	    		if (!checkAlphaNumeric(document.regForm.username.value)){
	    			$("#usernameErr").html("Must be alpha-numeric!");
	    			$("#usernameErr").css("color","#FF0000");
	    		}
	    	}else{
	    		$("#usernameErr").html("Such username already exists");
	    		$("#usernameErr").css("color","#FF0000");
	    	}
	    	});
	  });  
  
});